<?php

/**
 * 推广二维码
 */

namespace app\sale\middle;

class QrcodeMiddle extends \app\base\middle\BaseMiddle {


    protected function meta() {
        return parent::meta('我的二维码', '我的二维码');
    }

    private function saleInfo() {
        $userId = intval($this->params['user_id']);

        return target('sale/SaleUser')->getWhereInfo([
            'A.user_id' => $userId,
            'agent' => 1
        ]);
    }

    protected function data() {
        $userId = intval($this->params['user_id']);
        $userInfo = $this->params['user_info'];

        $saleInfo = $this->saleInfo();
        if (empty($saleInfo)) {
            return $this->stop('请先成为分销商!', 302, url('sale/Apply/index'));
        }
        $code = $saleInfo['code'];

        $path = 'upload/qrcode/' . $userId . '/';
        $dir = ROOT_PATH . $path;
        if (!is_dir($dir)) {
            if (!\mkdir($dir, 0777, true)) {
                return $this->stop('upload目录没有写入权限!');
            }
        }

        $shareFile = $dir . 'share.jpg';
        $shareUrl = url(VIEW_LAYER_NAME . '/index/Index/index', ['sale_code' => $code], true);

        $sharePath = ROOT_PATH . 'public/sale/images/share-user.png';
        $main = imagecreatefrompng($sharePath);
        $width = imagesx($main);

        $qrcodePath = $dir . "qrcode.png";
        \PHPQRCode\QRcode::png($shareUrl, $qrcodePath, 'L', 11, 1);

        //缩放二维码
        $dstCode = imagecreatefromstring(file_get_contents($qrcodePath));
        $qrcodeInfo = getimagesize($qrcodePath);
        $thumb = imagecreatetruecolor(300, 300);
        imagecopyresampled($thumb, $dstCode, 0, 0, 0, 0, 300, 300, $qrcodeInfo[0], $qrcodeInfo[1]);

        //写入二维码
        $dstShare = @imagecreatefromstring(file_get_contents($sharePath));
        imagecopy($dstShare, $thumb, 80, 491, 0, 0, 300, 300);
        imagedestroy($dstCode);
        imagedestroy($thumb);

        //写入头像
        if (!empty($userInfo['avatar'])) {
            $dstAvatar = imagecreatefromstring(file_get_contents($userInfo['avatar'] . '?type=3'));
            $avatarInfo = getimagesize($userInfo['avatar'] . '?type=3');
            $thumbAvatar = imagecreatetruecolor(128, 128);
            imagecopyresampled($thumbAvatar, $dstAvatar, 0, 0, 0, 0, 128, 128, $avatarInfo[0], $avatarInfo[1]);
            imagecopy($dstShare, $thumbAvatar, 42, 50, 0, 0, 128, 128);
            imagedestroy($dstAvatar);
        }

        //写入用户名
        $font = ROOT_PATH . 'public/common/fonts/adobehei.otf';
        $fontSize = 26;
        $fontColor = imagecolorallocate($dstShare, 255, 0, 0);
        //$fontBox = imagettfbbox($fontSize, 0, $font, $userInfo['show_name']);
        imagettftext($dstShare, $fontSize, 0, 266, 100, $fontColor, $font, $userInfo['show_name']);

        //写入网站标题
        $siteConfig = target('site/SiteConfig')->getConfig();
        $fontSize = 25;
        $str = '我为' . $siteConfig['info_name'] . '代言';
        $fontColor = imagecolorallocate($dstShare, 0, 0, 0);
        //$fontBox = imagettfbbox($fontSize, 0, $font, $str);
        imagefttext($dstShare, $fontSize, 0, 195, 155, $fontColor, $font, $str);

        //写入网站名称
        $white = imagecolorallocate($dstShare, 255, 255, 255);
        $text = '扫一扫，进入' . $siteConfig['info_name'] . '商城';
        $len = mb_strlen($text, "UTF-8");
        $left = (720 - $len * 32) / 2;
        imagefttext($dstShare, 26, 0, $left, 375, $white, $font, $text);

        //写入推荐码
        $fontSize = 20;
        $str = $code;
        $fontColor = imagecolorallocate($dstShare, 0, 0, 0);
        //$fontBox = imagettfbbox($fontSize, 0, $font, $str);
        imagefttext($dstShare, $fontSize, 0, 205, 845, $fontColor, $font, $str);

        ob_start();//启用输出缓存，暂时将要输出的内容缓存起来
        imagejpeg($dstShare, null, 100);//输出
        $poster = ob_get_contents();//获取刚才获取的缓存
        ob_end_clean();//清空缓存
        imagedestroy($dstShare);

        file_put_contents($shareFile, $poster);
        $shareImg = DOMAIN . ROOT_URL . '/' . $path . 'share.jpg?t=' . time();

        return $this->run([
            'share_img' => $shareImg,
            'share_url' => $shareUrl,
            'sale_code' => $code
        ]);
    }


}